package cn.sxt.userscenter.mana.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;

import cn.sxt.common.userscenter.util.ConstantFinalUtil;
import cn.sxt.common.userscenter.util.PageInfoUtil;
import cn.sxt.userscenter.mana.dao.IAAdminsDao;
import cn.sxt.userscenter.mana.dao.IARoleDao;
import cn.sxt.userscenter.mana.pojo.AAdmins;
import cn.sxt.userscenter.mana.pojo.ARole;
import cn.sxt.userscenter.mana.service.IUsersService;
import cn.sxt.userscenter.users.dao.IAUsersDao;
import cn.sxt.userscenter.users.dao.IAUsersExtendDao;
import cn.sxt.userscenter.users.pojo.AUsers;
import cn.sxt.userscenter.users.pojo.AUsersEnum;
import cn.sxt.userscenter.users.pojo.AUsersExtend;
import cn.sxt.userscenter.users.pojo.AUsersExtendEnum;
@Service("usersService")
public class UsersServiceImpl implements IUsersService{

	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	@Resource
	private IAUsersDao usersDao;
	@Resource
	private IAUsersExtendDao usersExtendDao;
	/*----------------------管理员模块开始------------------------*/
	@Override
	public AAdmins findOneAAdminsService(Map<String, Object> condMap) {
		return adminsDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {
		JSONObject resultJson = new JSONObject();
		int n = adminsDao.saveOne(admins);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", admins.getId()+"");
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJson = new JSONObject();
		int n = adminsDao.updateOne(admins);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", admins.getId()+"");
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins) {
		JSONObject resultJson = new JSONObject();
		int n = adminsDao.deleteOne(admins);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", admins.getId());
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return null;
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil!=null){
			Page<AAdmins> page = new Page<>(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAdmins> adminsList = adminsDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return adminsList;
		}
		return adminsDao.findCondList(condMap);
	}
	/*----------------------管理员模块结束-------------------------*/
	
	/*----------------------角色模块开始------------------------*/
	@Override
	public ARole findOneARoleService(Map<String, Object> condMap) {
		return roleDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneRoleService(ARole role) {
		JSONObject resultJson = new JSONObject();
		int n = roleDao.saveOne(role);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", role.getId()+"");
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role) {
		JSONObject resultJson = new JSONObject();
		int n = roleDao.updateOne(role);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", role.getId()+"");
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneRoleService(ARole role) {
		JSONObject resultJson = new JSONObject();
		int n = roleDao.deleteOne(role);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", role.getId());
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return null;
	}

	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil!=null){
			Page<ARole> page = new Page<>(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<ARole> roleList = roleDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return roleList;
		}
		return roleDao.findCondList(condMap);
	}
	/*----------------------角色模块结束-------------------------*/
	
	/*----------------------用户模块开始------------------------*/
	@Override
	public AUsers findOneAUsersService(Map<String, Object> condMap) {
		return usersDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneUsersService(AUsers users) {
		JSONObject resultJson = new JSONObject();
		/* 保存之前验证邮箱是否重复 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("email", users.getEmail());
		AUsers usersRes = this.usersDao.findOne(condMap);
		if(usersRes != null){
			/* 邮箱已经存在 */
			resultJson.put("code", "2");
			resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("2"));
			return resultJson;
		}
 		int n = usersDao.saveOne(users);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", users.getId()+"");
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
			/* 存储用户扩展信息 */
			AUsersExtend usersExtend = new AUsersExtend();
			usersExtend.setCreateTime(new Date());
			usersExtend.setUsersId(users.getId());
			usersExtend.setUpdateTime(new Date());
			this.saveOneUsersExtendService(usersExtend);
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneUsersService(AUsers users) {
		JSONObject resultJson = new JSONObject();
		int n = usersDao.updateOne(users);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", users.getId()+"");
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneUsersService(AUsers users) {
		JSONObject resultJson = new JSONObject();
		int n = usersDao.deleteOne(users);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", users.getId());
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return null;
	}

	@Override
	public List<AUsers> findCondListUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil!=null){
			Page<AUsers> page = new Page<>(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AUsers> usersList = usersDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return usersList;
		}
		return usersDao.findCondList(condMap);
	}
	/*----------------------用户模块结束-------------------------*/
	
	/*----------------------管理员模块开始------------------------*/
	@Override
	public AUsersExtend findOneAUsersExtendService(Map<String, Object> condMap) {
		return usersExtendDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneUsersExtendService(AUsersExtend usersExtend) {
		JSONObject resultJson = new JSONObject();
		int n = usersExtendDao.saveOne(usersExtend);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", usersExtend.getId()+"");
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneUsersExtendService(AUsersExtend usersExtend) {
		JSONObject resultJson = new JSONObject();
		int n = usersExtendDao.updateOne(usersExtend);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", usersExtend.getId()+"");
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneUsersExtendService(AUsersExtend usersExtend) {
		JSONObject resultJson = new JSONObject();
		int n = usersExtendDao.deleteOne(usersExtend);
		if(n>0){
			/* 存储结果 */
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", usersExtend.getId());
			dataJson.put("effectCount", n);
			resultJson.put("data", dataJson);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return null;
	}

	@Override
	public List<AUsersExtend> findCondListUsersExtendService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil!=null){
			Page<AUsersExtend> page = new Page<>(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AUsersExtend> usersExtendList = usersExtendDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return usersExtendList;
		}
		return usersExtendDao.findCondList(condMap);
	}
	/*----------------------管理员模块结束-------------------------*/
}
